<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '../../store/auth'

const router = useRouter()
const authStore = useAuthStore()

// 表单数据
const loginForm = reactive({
  username: 'admin',
  password: '123456'
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 控制状态
const loading = ref(false)
const loginFormRef = ref()

// 登录处理
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 使用新的登录方法
    await authStore.login({
      username: loginForm.username,
      password: loginForm.password
    })
    
    ElMessage.success('登录成功')
    
    // 获取重定向地址
    const redirect = router.currentRoute.value.query.redirect || '/dashboard'
    
    // 跳转到目标页面
    await router.push(redirect)
    
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}

// 快速登录方法
const quickLogin = (role) => {
  const accounts = {
    admin: { username: 'admin', password: '123456' },
    editor: { username: 'editor', password: '123456' },
    user: { username: 'user', password: '123456' }
  }
  
  if (accounts[role]) {
    Object.assign(loginForm, accounts[role])
    handleLogin()
  }
}

// 演示账号数据
const demoAccounts = ref([
  {
    role: 'admin',
    label: '管理员账号',
    type: 'danger',
    icon: 'UserFilled'
  },
  {
    role: 'editor', 
    label: '编辑员账号',
    type: 'warning',
    icon: 'Edit'
  },
  {
    role: 'user',
    label: '普通用户',
    type: 'info', 
    icon: 'User'
  }
])
</script>

<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="bg-circle bg-circle-1"></div>
      <div class="bg-circle bg-circle-2"></div>
      <div class="bg-circle bg-circle-3"></div>
    </div>
    
    <div class="login-wrapper">
      <!-- 左侧信息区域 -->
      <div class="login-info">
        <div class="info-content">
          <h1 class="system-title">Vue3 权限管理系统</h1>
          <p class="system-subtitle">现代化的前端权限管理解决方案</p>
          <div class="features">
            <div class="feature-item">
              <div class="feature-icon">
                <el-icon size="24"><Shield /></el-icon>
              </div>
              <div class="feature-text">
                <h4>安全权限控制</h4>
                <p>细粒度的权限管理，保障系统安全</p>
              </div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <el-icon size="24"><Monitor /></el-icon>
              </div>
              <div class="feature-text">
                <h4>响应式设计</h4>
                <p>适配各种设备，优雅的用户体验</p>
              </div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <el-icon size="24"><Lightning /></el-icon>
              </div>
              <div class="feature-text">
                <h4>高性能架构</h4>
                <p>基于Vue3+Vite，极速开发体验</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-form-wrapper">
        <div class="login-form-container">
          <div class="form-header">
            <h2>欢迎登录</h2>
            <p>请输入您的账号信息</p>
          </div>
          
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="rules"
            class="login-form"
            size="large"
            @keyup.enter="handleLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                prefix-icon="User"
                clearable
                class="form-input"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
                clearable
                class="form-input"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                :loading="loading"
                class="login-button"
                @click="handleLogin"
              >
                {{ loading ? '登录中...' : '立即登录' }}
              </el-button>
            </el-form-item>
          </el-form>
          
          <!-- 快速登录演示 -->
          <div class="quick-login">
            <div class="divider">
              <span>演示账号</span>
            </div>
            <div class="demo-accounts">
              <el-button 
                v-for="account in demoAccounts" 
                :key="account.role"
                :type="account.type"
                size="default"
                plain
                class="demo-btn"
                @click="quickLogin(account.role)"
              >
                <el-icon class="demo-icon"><component :is="account.icon" /></el-icon>
                {{ account.label }}
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  min-height: 100vh;
  width: 100vw;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
}

/* 背景装饰 */
.bg-decoration {
  display: none;
}

.bg-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.bg-circle-1 {
  width: 200px;
  height: 200px;
  top: -100px;
  right: -100px;
  animation: float 6s ease-in-out infinite;
}

.bg-circle-2 {
  width: 150px;
  height: 150px;
  bottom: -75px;
  left: -75px;
  animation: float 8s ease-in-out infinite reverse;
}

.bg-circle-3 {
  width: 100px;
  height: 100px;
  top: 50%;
  left: 10%;
  animation: float 10s ease-in-out infinite;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 主要内容区域 */
.login-wrapper {
  position: relative;
  z-index: 2;
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 40px;
}

/* 左侧信息区域 */
.login-info {
  flex: 1;
  padding-right: 60px;
  color: white;
}

.info-content {
  max-width: 500px;
}

.system-title {
  font-size: 3.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
  background: linear-gradient(45deg, #fff, #f0f9ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  line-height: 1.2;
}

.system-subtitle {
  font-size: 1.25rem;
  margin-bottom: 3rem;
  opacity: 0.9;
  line-height: 1.6;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.5rem;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.feature-item:hover {
  transform: translateY(-2px);
  background: rgba(255, 255, 255, 0.15);
}

.feature-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.feature-text h4 {
  margin: 0 0 0.5rem;
  font-size: 1.1rem;
  font-weight: 600;
}

.feature-text p {
  margin: 0;
  opacity: 0.8;
  font-size: 0.9rem;
  line-height: 1.5;
}

/* 右侧登录表单 */
.login-form-wrapper {
  width: 500px;
  max-width: 500px;
  flex-shrink: 0;
}

.login-form-container {
  background: white;
  border-radius: 24px;
  padding: 3rem;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.form-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.form-header h2 {
  font-size: 2rem;
  font-weight: 600;
  color: #1a202c;
  margin-bottom: 0.5rem;
}

.form-header p {
  color: #64748b;
  font-size: 1rem;
  margin: 0;
}

.login-form {
  margin-bottom: 2rem;
}

.form-input {
  margin-bottom: 1.5rem;
}

:deep(.form-input .el-input__wrapper) {
  padding: 12px 16px;
  border-radius: 12px;
  border: 2px solid #e2e8f0;
  box-shadow: none;
  transition: all 0.3s ease;
}

:deep(.form-input .el-input__wrapper:hover) {
  border-color: #cbd5e1;
}

:deep(.form-input .el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.login-button {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

/* 快速登录区域 */
.quick-login {
  margin-top: 2rem;
}

.divider {
  position: relative;
  text-align: center;
  margin-bottom: 1.5rem;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e2e8f0;
}

.divider span {
  background: white;
  color: #64748b;
  padding: 0 1rem;
  font-size: 0.875rem;
}

.demo-accounts {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.demo-btn {
  justify-content: flex-start;
  border-radius: 8px;
  height: 40px;
  transition: all 0.2s ease;
}

.demo-btn:hover {
  transform: translateX(4px);
}

.demo-icon {
  margin-right: 8px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-wrapper {
    flex-direction: column;
    padding: 2rem;
    gap: 3rem;
    justify-content: center;
  }
  
  .login-info {
    padding-right: 0;
    text-align: center;
    max-width: 100%;
  }
  
  .system-title {
    font-size: 2.5rem;
  }
  
  .features {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 1rem;
    justify-content: center;
  }
  
  .feature-item {
    flex: 1;
    min-width: 250px;
    max-width: 300px;
  }
  
  .login-form-wrapper {
    width: 100%;
    max-width: 450px;
  }
}

@media (max-width: 768px) {
  .login-container {
    position: relative;
  }
  
  .login-wrapper {
    padding: 1rem;
    min-height: 100vh;
  }
  
  .login-form-container {
    padding: 2rem;
  }
  
  .system-title {
    font-size: 2rem;
  }
  
  .features {
    flex-direction: column;
  }
  
  .feature-item {
    min-width: auto;
    max-width: none;
  }
}
</style>